/**
 * Created by linx on 2016/12/15.
 */
require('easyui')
var $alert = require("libs/alert")
var $confirm = require("libs/confirm")
var isSale = require('libs/user-type').isSale;
$(function () {
    logisticsInit();
    providerInit();
    tableinit();
    $("body").on("click","a[name=tabOpen]",function () {
        var id = $(this).attr('row');
        var text = $("#tab_open"+id).text();
        if(text=='展开'){
            $("#box"+id).show();
            $("#tab_open"+id).text("隐藏");
        }else{
            $("#box"+id).hide();
            $("#tab_open"+id).text("展开");
        }
    });
    $("body").on("click","a[name=remove]",function () {
        var id = $(this).attr('row');

        $confirm("您确定要删除该物流产品和关联物流路线吗？删除后，该物流产品和关联物流路线将永久删除。",function () {
            $.ajax({
                url:'api/v1/p/mms/logistics/delete',
                type:'post',
                data:{
                    id:id
                },
                dataType:'json',
                success:function (result) {
                    if(result.code == 200){
                        $(".product-list").remove();
                        tableinit();
                    }else{
                        $alert(result.message);
                    }
                },
                error:function (e) {
                    $alert("物流产品删除失败");
                }
            })
        });
    });
    $("#logistics_search").on('click',function () {
        $(".product-list").remove();
        tableinit();
    });
});

function tableinit() {
    var logisticsId = $('#logisticsId').combobox('getValue');
    var providerId = $('#providerId').combobox('getValue');
    console.log(logisticsId+","+providerId)
    $.ajax({
        url:'api/v1/p/mms/logistics/list',
        type:'post',
        data:{
            logisticsId:logisticsId,
            providerId:providerId
        },
        dataType:'json',
        success:function (result) {
            if(result.code == 200){
                var html ='';
                $.each(result.data.rows,function (index,obj) {
                    html +="<div class='product-list'><div class='title'><span class='name'>"+obj.name+"</span> <span><a href='javascript:;' id='tab_open"+index+"' name='tabOpen' row='"+index+"'>展开</a></span>" +
                        " <span class='right'><a href='logisticsUpdate?id="+obj.id+"'>修改</a> |<a href='javascript:;' name='remove' row='"+obj.id+"'>删除</a></span> </div> <div style='display: none' class='tablebox' id='box"+index+"'> <table class='table'> <thead> <tr> <th>路线名称</th> <th>物流渠道名称</th> </tr> </thead> <tbody>";
                    $.each(obj.channels,function (indexC,objC) {
                        html+="<tr> <td>"+objC.name+"</td> <td>"+objC.providerName +"</td> </tr>";
                    });
                    html+="</tbody> </table> </div> </div>"
                })
                $('.search-box').after(html);
            }else{
                $alert(result.message);
            }
        },
        error:function (e) {
            $alert("物流产品查询失败");
        }
    })
}


function logisticsInit() {
    $("#logisticsId").combobox({
        url:'api/v1/p/mms/logistics/query',
        valueField:'id',
        textField:'name',
        loadFilter:function(result){
            if(result.code=='200'){
               result.data.unshift({id:'',name:'全部'});
                return result.data;
            }
        }
    })
}


function providerInit() {
    $("#providerId").combobox({
        url:'api/v1/p/mms/internationalLogisticsProvider/query',
        valueField:'id',
        textField:'code',
        loadFilter:function(result){
            if(result.code=='200'){
                result.data.unshift({id:'',code:'全部'});
                return result.data;
            }
        }
    })
}

